import java.net.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.*;
import javax.swing.*;

public final class StdDraw {

	public static final Color BLACK = Color.BLACK;
	public static final Color GRAY = Color.GRAY;
	public static final Color WHITE = Color.WHITE;

	private static final int DEFAULT_SIZE = 512;
	private static int width = DEFAULT_SIZE;
	private static int height = DEFAULT_SIZE;

	private static final double BORDER = 0.05;
	private static double xmin, ymin, xmax, ymax;

	private static BufferedImage offscreenImage, onscreenImage;
	private static Graphics2D offscreen, onscreen;
	private static JFrame frame;

	private static final Font DEFAULT_FONT = new Font("SansSerif", Font.PLAIN,
			100);

	public static void setCanvasSize(int w, int h) {
		if (w < 1 || h < 1)
			throw new RuntimeException("width and height must be positive");
		width = w;
		height = h;
		init();
	}

	private static void init() {
		if (frame != null)
			frame.setVisible(false);
		frame = new JFrame();
		offscreenImage = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_ARGB);
		onscreenImage = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_ARGB);
		offscreen = offscreenImage.createGraphics();
		onscreen = onscreenImage.createGraphics();
		offscreen.setColor(Color.WHITE);
		offscreen.fillRect(0, 0, width, height);
		clear();

		ImageIcon icon = new ImageIcon(onscreenImage);
		JLabel draw = new JLabel(icon);

		frame.setContentPane(draw);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle("Basketball Totten");
		frame.setJMenuBar(createMenuBar());
		frame.pack();
		frame.setVisible(true);
	}

	public static JMenuBar createMenuBar() {
		JMenuBar menuBar = new JMenuBar();
		JMenu menu = new JMenu("Game");
		menuBar.add(menu);
		JMenuItem menuItem1 = new JMenuItem("Show Rules...");
		menu.add(menuItem1);
		menuItem1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Rules.main(null);
			}
		});
		JMenuItem menuItem2 = new JMenuItem("Quit...");
		menu.add(menuItem2);
		menuItem2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		return menuBar;
	}

	public static void setXscale(double min, double max) {
		double size = max - min;
		xmin = min - BORDER * size;
		xmax = max + BORDER * size;
	}

	public static void setYscale(double min, double max) {
		double size = max - min;
		ymin = min - BORDER * size;
		ymax = max + BORDER * size;
	}

	private static double scaleX(double x) {
		return width * (x - xmin) / (xmax - xmin);
	}

	private static double scaleY(double y) {
		return height * (ymax - y) / (ymax - ymin);
	}

	public static void clear() {
		clear(Color.WHITE);
	}

	public static void clear(Color color) {
		offscreen.setColor(color);
		offscreen.fillRect(0, 0, width, height);
		draw();
	}

	public static void updateDeck() {
		offscreen.setColor(BLACK);
		offscreen.fillRect(0, 0, 150, 150);
		draw();
	}

	public static void updateStone(int x, int y) {
		offscreen.setColor(BLACK);
		offscreen.fillRect(x, y, 60, 60);
		draw();
	}

	// get an image from the given filename
	private static Image getImage(String filename) {

		// to read from file
		ImageIcon icon = new ImageIcon(filename);

		// try to read from URL
		if ((icon == null)
				|| (icon.getImageLoadStatus() != MediaTracker.COMPLETE)) {
			try {
				URL url = new URL(filename);
				icon = new ImageIcon(url);
			} catch (Exception e) { /* not a url */
			}
		}

		// in case file is inside a .jar
		if ((icon == null)
				|| (icon.getImageLoadStatus() != MediaTracker.COMPLETE)) {
			URL url = StdDraw.class.getResource(filename);
			if (url == null)
				throw new RuntimeException("image " + filename + " not found");
			icon = new ImageIcon(url);
		}

		return icon.getImage();
	}

	// 12345
	public static void picture(double x, double y, String s) {
		Image image = getImage(s);
		double xs = scaleX(x);
		double ys = scaleY(y);
		int ws = image.getWidth(null);
		int hs = image.getHeight(null);
		if (ws < 0 || hs < 0)
			throw new RuntimeException(s + " is  a corrupt imagez");

		offscreen.drawImage(image, (int) Math.round(xs - ws / 2.0), (int) Math
				.round(ys - hs / 2.0), null);
		draw();
	}

	// 12345

	public static void setPenColor(Color color) {
		offscreen.setColor(color);
	}

	public static void text(double x, double y, String s) {
		offscreen.setFont(DEFAULT_FONT);
		FontMetrics metrics = offscreen.getFontMetrics();
		double xs = scaleX(x);
		double ys = scaleY(y);
		int ws = metrics.stringWidth(s);
		int hs = metrics.getDescent();
		offscreen.drawString(s, (float) (xs - ws / 2.0), (float) (ys + hs));
		draw();
	}

	public static void show(int t) {
		draw();
	}

	public static void show() {
		draw();
	}

	static void draw() {
		onscreen.drawImage(offscreenImage, 0, 0, null);
		frame.repaint();
	}

	public static void main(String[] args) {
	}

}
